<?php

namespace Erp\ErpBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Erp\ErpBundle\Entity\Qcm;
use Erp\ErpBundle\Entity\Qcm_Reponse;
use Erp\ErpBundle\Entity\Qcm_Question;
// use Erp\ErpBundle\Entity\Participer_Evenement;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use JMS\SecurityExtraBundle\Annotation\Secure;
use Symfony\Component\HttpFoundation\Response;

class QcmController extends Controller
{    	
	public function gererAction($requete)
	{
		$em = $this->getDoctrine()->getEntityManager();
		$em->persist($requete);
		$em->flush();
	}
    
    /**
     * @Secure(roles="ROLE_PROFESSEUR")
     */
    public function listerqcmAction()
    {
	
    	$qcms = $this->getDoctrine()
					 ->getEntityManager()
					 ->getRepository('ErpBundle:Qcm')
					 ->findAll();
					 
    	$qcms = $this->getDoctrine()
					 ->getEntityManager()
					 ->getRepository('ErpBundle:Qcm_Reponse')
					 ->findAll();
					 
    	$qcms = $this->getDoctrine()
					 ->getEntityManager()
					 ->getRepository('ErpBundle:Qcm_Question')
					 ->findAll();
						 
		if (isset($_REQUEST['text'])){
			$txt = str_replace("\r\n","\n",$_REQUEST['text']);
			$txt = explode("\n", $txt);
			
			$data = array();
			$type = 'question';
			$indice = 0;
			foreach($txt as $l){
			
				$l = trim($l);
				if ( $l == '')
					continue;
						
				if ($type == 'question') {
					if ( $l[0] == '-' || $l[0] == '='){
						$type = 'reponses';
						$data[$indice]['reponses'][] = $l;
						continue;
					}
	
					if (isset($data[$indice]['question']))
						$data[$indice]['question'] .= '<br/>' . $l;
					else
						$data[$indice]['question'] = $l;
				}
				else{
					if ( $l[0] != '-' && $l[0] != '='){
						$type = 'question';
						$indice ++;
						$data[$indice]['question'] = $l;
						continue;
					}
					
					$data[$indice]['reponses'][] = $l;
				}
			}
			return $this->render('ErpBundle:Qcm:list.html.twig', array( 'query' => $_REQUEST['text'], 'data' => $data , 'qcm' => $qcms ));
		}
		else
			return $this->render('ErpBundle:Qcm:list.html.twig', array( 'query' => '', 'data' => '' , 'qcm' => $qcms ));
	
		// return $this->render('ErpBundle:Qcm:list.html.twig', array( 'event' => $event ));
    }
}
